import { $createUid } from "@/utils"
class Obj {
  lib = {}
  key = ''
  uid = null
  constructor(obj, key) {
    if (!obj || typeof obj !== 'object') return Error('[class Obj : the params "obj" is not a object]')
    else this.lib = { ...obj }
    if (key) {
      if (typeof key !== 'string') return Error('[class Obj : the params "key" is not a string]')
      else this.key = key
    }
    this.uid = $createUid()
  }
  has(key) { return Object.hasOwn(this.lib, key) }
  add(obj, key) {
    if (key) this.lib[key] = obj
    else if (obj[this.key]) this.lib[obj[this.key]] = obj
  }
  addFromArray(arr) {
    // console.log(arr)
    arr.forEach(i => {
      if (i[this.key]) this.lib[i[this.key]] = i
      else console.error(`[class Obj addFromArray: some items has not the key "${this.key}" from params`)
    })
  }
  delete(key) {
    if (!this.lib[key]) return console.error(`[class Obj addFromArray: some items has not the key "${key}" from params`)
    delete this.lib[key]
  }
  clear() {
    this.lib = {}
  }
  push(obj) {
    this.lib = Object.assign(this.lib, obj)
  }
  keys() { return Object.keys(this.lib) }
  values() { return Object.keys(this.lib).map(i => this.lib[i]) }
  get(key) { return this.lib[key] || NaN }
  toObject() {
    return this.lib
  }
}
// (function () {
//   window.Obj = Obj
// }())
// Object.defineProperty(window.prototype, 'Obj', {
//   value: function () {
//     return Obj;
//   },
//   enumerable: false,
//   writable: false,
//   configurable: true,
// })
// window.prototype.Obj = Obj
window.Obj = Obj
export default Obj
